<template>
  <u-modal
    v-model="visible"
    title="选择城市活动"
    showCancelButton
    showConfirmButton
    confirm-color="rgb(41, 121, 255)"
    @cancel="onCancel"
    @confirm="onConfirm"
  >
    <view class="activity-list">
        <view class="activity-item" :class="{ 'active': activity.active }" v-for="(activity, index) in activityList"
      :key="index" @click="onActivityClick(activity)">
        <view class="activity-item-content">
          <view class="activity-cell">
            <view class="label">名称：</view>
            <view class="content">{{ activity.name }}</view>
          </view>
          <view class="activity-cell">
            <view class="label">价格：</view>
            <view class="content">{{ activity.price }}元</view>
          </view>
        </view>
        <view class="activity-item-radio">
          <u-icon name="checkbox-mark" color="#fff" size="20"></u-icon>
        </view>
      </view>
    </view>
  </u-modal>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      activityList: [],
    };
  },
  methods: {
    open(activityList) {
      let tempActivityList = JSON.parse(JSON.stringify(activityList))
      this.visible = true;
      this.activityList = tempActivityList
    },
    onActivityClick(activity) {
      let beforeActive = activity.active
      this.activityList.forEach(item => {
        item.active = false
      })
      activity.active = !beforeActive
    },
    onCancel() {
      this.visible = false
      this.$emit('onCancel')
    },
    onConfirm() {
      let active = this.activityList.find(item => item.active)
      this.visible = false
      if (active) {
        this.$emit('onConfirm', active)
      } else {
        this.$emit('onConfirm')
      }
    }
  },
};
</script>
<style lang="less" scoped>
.activity-list {
  padding: 40rpx 20rpx;
  max-height: 60vh;
  overflow: auto;
  .activity-item {
    display: flex;
    align-items: center;
    line-height: 20px;
    border: 1px solid #bdbcbc;
    padding: 6px 10px;
    font-size: 14px;
    justify-content: space-between;
    .activity-cell {
      display: flex;
      .content {
        flex: 1;
        color: #4d4a4a;
      }
    }
    .activity-cell + .activity-cell {
      margin-top: 10px;
    }
    .activity-item-radio {
      color: #606266;
      display: flex;
      flex-direction: row;
      flex: none;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      width: 17px;
      height: 17px;
      text-align: center;
      font-size: 20px;
      border: 1px solid #c8c9cc;
      border-radius: 100%;
      margin-left: 6px;
    }
    &.active {
      .activity-item-radio {
        border-color: rgb(41, 121, 255);
        background-color: rgb(41, 121, 255);
      }
    }
  }
  .activity-item + .activity-item {
    margin-top: 10px;
  }
}

</style>
